package P004_ElongateOverLoad;

import org.junit.Test;

import java.text.NumberFormat;

/**
 * 避免带有变长参数的方法重载
 * @author xufy1
 */
public class ElongateOverLoad {

    @Test
    public  void test1() {
        ElongateOverLoad demo4 = new ElongateOverLoad();
        //int 是基本数据类型，编译器默认先根据是否是基本数据类型判断，然后再判断是否是数组，只要满足条件就通过
        demo4.calPrice(49900, 75);
//        demo4.calPrice(49900,75,50);
    }

    public void calPrice(int price, int discount) {
        float knockdownPrice = price * discount / 100;
        System.out.println("简单折扣后价格：" + formateCurrency(knockdownPrice));
    }

    public void calPrice(int price, int... discounts) {
        float knockdownPrice = price;
        for (int discount : discounts) {
            knockdownPrice = price * discount / 100;
        }
        System.out.println("复杂折扣后价格：" + formateCurrency(knockdownPrice));
    }


    private String formateCurrency(float price) {
        return NumberFormat.getCurrencyInstance().format(price / 100);
    }
}
